/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;

namespace DaveSexton.DocProject.Sandcastle.TopicManagement
{
	internal sealed class TopicEditorToolWindow : ToolWindow
	{
		#region Public Properties
		public static readonly Guid ToolWindowGuid = new Guid("{112C6F5A-0A47-4907-B6CA-A639DF2F4026}");
		public static readonly TopicEditorToolWindow Instance = new TopicEditorToolWindow();

		public override System.Windows.Forms.Control Control
		{
			get
			{
				return control;
			}
		}

		public override string Caption
		{
			get
			{
				IDocProject currentProject = TopicExplorer.CurrentProject;
				Topic currentTopic = TopicExplorer.CurrentTopic;

				if (currentProject == null)
					return Resources.Text.TopicEditorToolWindowCaption;
				else if (currentTopic != null)
					return string.Format(System.Globalization.CultureInfo.CurrentCulture, "{0}: {1}",
						currentProject.Name, currentTopic.TocTitle);
				else
					return string.Format(System.Globalization.CultureInfo.CurrentCulture, "{0} - {1}", 
						Resources.Text.TopicEditorToolWindowCaption, currentProject.Name);
			}
		}

		public override System.Drawing.Bitmap Image
		{
			get
			{
				return Resources.Images.TopicManagement_ToolWindowIcon;
			}
		}
		#endregion

		#region Private / Protected
		private readonly TopicEditorControl control = new TopicEditorControl();
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="TopicEditorToolWindow" /> class.
		/// </summary>
		private TopicEditorToolWindow()
			: base(ToolWindowGuid)
		{
			control.Hierarchy = TopicExplorer.Instance;
		}
		#endregion

		#region Methods
		public void SyncWithTopicExplorer()
		{
			Window.Caption = Caption;

			control.Manager = TopicExplorer.CurrentManager;

			Topic topic = TopicExplorer.CurrentTopic;

			if (topic == null || topic.IsApiPlaceholder)
				control.Topic = null;
			else
				control.Topic = topic;
		}
		#endregion

		#region Event Handlers
		protected override void OnShow(EventArgs e)
		{
			TopicExplorer.CurrentTopicChanged += TopicExplorer_CurrentTopicChanged;

			SyncWithTopicExplorer();

			base.OnShow(e);
		}

		protected override void OnHide(EventArgs e)
		{
			TopicExplorer.CurrentTopicChanged -= TopicExplorer_CurrentTopicChanged;

			if (control.Manager != null)
				control.Manager.SaveChanges(TopicManagerChanges.Documentation);

			base.OnHide(e);
		}

		private void TopicExplorer_CurrentTopicChanged(object sender, EventArgs e)
		{
			SyncWithTopicExplorer();
		}
		#endregion
	}
}
